package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.PermissionDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * 权限管理
 */
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分页查询所有权限
     * @param currentPage
     * @param pageSize
     * @param queryString
     * @return
     */
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        //1.设置分页参数
        PageHelper.startPage(currentPage,pageSize);
        //2.需要分页的语句
        Page<Permission> permissionDaoPage = permissionDao.selectByCondition(queryString);
        return new PageResult(permissionDaoPage.getTotal(),permissionDaoPage.getResult());
    }


    /**
     * 新增权限
     * @param permission
     */
    @Override
    public void add(Permission permission) {
        permissionDao.add(permission);
    }

    /**
     * 删除权限
     * @param id
     */
    @Override
    public void delete(Integer id) {
       int count =  permissionDao.findRoleIdByPerId(id);
       if (count > 0){
           throw new RuntimeException(MessageConstant.DELETE_ROLEANDPER_FAIL);
       }else {
           permissionDao.delete(id);
       }
    }

    /**
     * 根据id查询权限数据编辑时回显
     * @param id
     */
    @Override
    public Permission findById(Integer id) {
       return permissionDao.findById(id);
    }

    /**
     * 编辑权限
     * @param permission
     */
    @Override
    public void edit(Permission permission) {
        permissionDao.edit(permission);
    }
}
